package com.stx.bms.web;

import com.stx.bms.dao.BlogDAO;
import com.stx.bms.dao.BlogDAOImpl;
import com.stx.bms.entity.User;
import com.stx.bms.util.FileUploadUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/admin/blog-add")
@MultipartConfig
public class BlogAddServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String accessPath = FileUploadUtil.uploadFile(request, "file");

        //获取前端的参数
        String title = request.getParameter("title");
        String content = request.getParameter("content");
        System.out.println(content);

        //获取当前登录用户
        User currUser = (User)request.getSession().getAttribute("loginUser");

        //调用DAO插入数据
        BlogDAO dao = new BlogDAOImpl();
        dao.add(title, content, currUser.getId(), accessPath);

        //跳转到列表页面(请求转发，重定向)
        response.sendRedirect("blog-list");
    }

}
